/**
 * 
 */
package com.cqan.main.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cqan.account.entity.Permission;
import com.cqan.account.entity.Resource;
import com.cqan.account.entity.Resource.ResourceType;
import com.cqan.account.entity.Role;
import com.cqan.account.entity.User;
import com.cqan.account.service.PermissionService;
import com.cqan.account.service.ResourceService;
import com.cqan.account.service.RoleService;
import com.cqan.account.service.UserService;
import com.cqan.common.ViewName;
import com.cqan.exception.AppException;
import com.cqan.utils.CodeUtils;

/**
 * @Description TODO
 * @author wuhui
 * @date 2012-4-27下午4:16:00
 * 
 */
@Controller
@RequestMapping("/admin")
public class MainController {

	private static Logger logger = Logger.getLogger("ds.debug");

	private ResourceService resourceService;

	private UserService userService;

	private RoleService roleService;

	private PermissionService permissionService;

	/**
	 * 映射首页
	 * 
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public ModelAndView main() throws AppException {
		ModelAndView view = new ModelAndView(ViewName.MAIN_HOME);
		Object obj = SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		if (obj instanceof String) {
			return view;
		}
		User user = (User) obj;
		List<Resource> resources = new ArrayList<Resource>();
		List<Role> roles = user.getRoles();
		for (Role role : roles) {
			List<Permission> permissions = role.getPermissions();
			if (permissions != null && permissions.size() > 0) {
				for (Permission permission : permissions) {
					Resource resource = permission.getResource();
					if (resource != null
							&& ResourceType.MENU.equals(resource.getType())) {
						resources.add(resource);
					}
				}
			}
		}
		view.addObject("menu", resources);
		return view;
	}


	/**
	 * 生成验证码
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/code", method = RequestMethod.GET)
	public ModelAndView code(HttpServletRequest request,
			HttpServletResponse response) throws AppException {
		int width = 55;
		int height = 23;
		try {
			BufferedImage image = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = image.getGraphics();
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, width, height);
			g.setFont(new Font("Arial", Font.PLAIN, 18));
			String code = RandomStringUtils.random(4, true, true);
			for (int i = 0; i < code.length(); i++) {
				g.setColor(CodeUtils.getRandomColor());
				g.drawString(String.valueOf(code.charAt(i)), (int)((width * 0.23) * (i+0.2)),(int) (height * 0.8));
			}
			request.getSession().setAttribute("validateCode",code);
			logger.debug("validateCode is " +code);
			g.dispose();
			ServletOutputStream output = response.getOutputStream();
			ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
			ImageIO.write(image, "JPEG", imageOut);
			imageOut.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new AppException("account.validateCode");
		}
		return null;
	}

	@Autowired
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Autowired
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	@Autowired
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

}
